<?php 

class ArticlePresenter extends BasePresenter{
	
	private $model;
	/**
	 * Start lifecycle presenter
	 *
	 * @return void
	 */
	protected function startup(){
		parent::startup();
		
		$this->model = ArticleModel::getInstance();
	}

	/**
	 * Settings for all render
	 *
	 * @return void
	 */
	protected function beforeRender(){

	}
	
	public function boxHTML($id){

		$this->model = ArticleModel::getInstance();
		
		$system = SystemModel::getInstance();
		$menuItem = $system->getMenuItemByName($id);
		$news = $this->model->getNews(array("id_menu" => $menuItem[0]["id_menu"]), 6);
		
		foreach($news as &$n){
			$n["datum"] = date("d.m.Y", strtotime($n["date"])); 
			$n["link"] = $this->link("Article:", array("id"=>$id, "seo_title"=>$n["seo_title"]));
			$n["picture"] = $this->model->getPicture($n["id_file"]);
		}
		
		$link = $this->link("Article:", array("id"=>$id));
		
		$template = new Template();
		$template->registerFilter(new LatteFilter);
		$template->news = $news;
		$template->link = $link;
		$template->setFile(APP_DIR."/modules/Article/templates/front/box.phtml");
		
		return $template;
	}
	
	/**
	 * Render default action
	 *
	 * @return void
	 */
	public function renderDefault($id){
		
		$menuItem = $this->system->getMenuItemByName($id);
		$par = $this->getRequest()->getParams();
		if(!empty($par["seo_title"])){
			
			$address = $this->getPresenter()->link("Article:", array("id" => $id, "seo_title" => $par["seo_title"]));
			
			$name = $this->model->getNewByName($par["seo_title"]);
			$name["picture"] = $this->model->getPicture($name["id_file"]);
			
			$navigace = array(
								"address" => $address,
								"name" => $name["title"]
								);
			
			$this->addToNavigation($navigace);			
			
			$this->template->new = $name;
		}else{

			$this->template->news_view = $this->model->getNews(array("id_menu" => $menuItem[0]["id_menu"]));
		}
		
		$this->template->id = $id;
	}
	
	public function actionViewNew(){
		
	}
}

?>
